package com.neu.carbon.mes.service.impl;

import com.neu.carbon.mes.domain.MesProductSchedule;
import com.neu.carbon.mes.domain.MesProductScheduleMaterial;
import com.neu.carbon.mes.mapper.MesProductScheduleMapper;
import com.neu.carbon.mes.service.IMesProductScheduleService;
import com.neu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 计划排产Service业务层处理
 *
 * @author neuedu
 * @date 2025-02-28
 */
@Service
public class MesProductScheduleServiceImpl implements IMesProductScheduleService {
    @Autowired
    private MesProductScheduleMapper mesProductScheduleMapper;

    /**
     * 查询计划排产
     *
     * @param id 计划排产ID
     * @return 计划排产
     */
    @Override
    public MesProductSchedule selectMesProductScheduleById(Long id) {
        return mesProductScheduleMapper.selectMesProductScheduleById(id);
    }

    /**
     * 查询计划排产列表
     *
     * @param mesProductSchedule 计划排产
     * @return 计划排产
     */
    @Override
    public List<MesProductSchedule> selectMesProductScheduleList(MesProductSchedule mesProductSchedule) {
        List<MesProductSchedule> mesProductSchedules = mesProductScheduleMapper.selectMesProductScheduleList(mesProductSchedule);

        return mesProductSchedules;
    }

    /**
     * 新增计划排产
     *
     * @param mesProductSchedule 计划排产
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesProductSchedule(MesProductSchedule mesProductSchedule) {
        mesProductSchedule.setSerialNo(generateSerialNo());
        int rows = mesProductScheduleMapper.insertMesProductSchedule(mesProductSchedule);
        insertMesProductScheduleMaterial(mesProductSchedule);
        return rows;
    }

    /**
     * 修改计划排产
     *
     * @param mesProductSchedule 计划排产
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesProductSchedule(MesProductSchedule mesProductSchedule) {
        mesProductScheduleMapper.deleteMesProductScheduleMaterialByScheduleId(mesProductSchedule.getId());
        insertMesProductScheduleMaterial(mesProductSchedule);
        return mesProductScheduleMapper.updateMesProductSchedule(mesProductSchedule);
    }

    /**
     * 批量删除计划排产
     *
     * @param ids 需要删除的计划排产ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesProductScheduleByIds(Long[] ids) {
        mesProductScheduleMapper.deleteMesProductScheduleMaterialByScheduleIds(ids);
        return mesProductScheduleMapper.deleteMesProductScheduleByIds(ids);
    }

    /**
     * 删除计划排产信息
     *
     * @param id 计划排产ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesProductScheduleById(Long id) {
        mesProductScheduleMapper.deleteMesProductScheduleMaterialByScheduleId(id);
        return mesProductScheduleMapper.deleteMesProductScheduleById(id);
    }

    @Override
    public Map<String, List<Map<String, Long>>> getFormSelect() {
        Map<String, List<Map<String, Long>>> formSelect = new HashMap<>();
        formSelect.put("plan", mesProductScheduleMapper.getPlan());
        formSelect.put("project", mesProductScheduleMapper.getProject());
        formSelect.put("product", mesProductScheduleMapper.getProduct());
        formSelect.put("bom", mesProductScheduleMapper.getBom());
        formSelect.put("productLine", mesProductScheduleMapper.getProductLine());
        formSelect.put("process", mesProductScheduleMapper.getProcess());
        return formSelect;
    }

    /**
     * 新增排产物料信息信息
     *
     * @param mesProductSchedule 计划排产对象
     */
    public void insertMesProductScheduleMaterial(MesProductSchedule mesProductSchedule) {
        List<MesProductScheduleMaterial> mesProductScheduleMaterialList = mesProductSchedule.getMesProductScheduleMaterialList();
        Long id = mesProductSchedule.getId();
        if (StringUtils.isNotNull(mesProductScheduleMaterialList)) {
            List<MesProductScheduleMaterial> list = new ArrayList<>();
            for (MesProductScheduleMaterial mesProductScheduleMaterial : mesProductScheduleMaterialList) {
                mesProductScheduleMaterial.setScheduleId(id);
                list.add(mesProductScheduleMaterial);
            }
            if (list.size() > 0) {
                mesProductScheduleMapper.batchMesProductScheduleMaterial(list);
            }
        }
    }

    private String generateSerialNo() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String datePart = currentDate.format(formatter);
        // 格式化ID为8位数字，不足8位前面补0
        String idPart = String.format("%07d", new Random().nextInt(10) + 1);
        // 前缀
        String prefix = "SCJH";
        // 拼接最终的serialNo
        return prefix + datePart + idPart;
    }
}
